﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Layer;
using DTO;
using System.Data;
public partial class NNC_H_QL_GiangHang : System.Web.UI.Page
{
    GiangHangBUS ghBUS = new GiangHangBUS();
    string userName = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["NCC_UN"] == null)
        {
            Response.Redirect("Login.aspx");
            return;
        }

        userName = Session["NCC_UN"].ToString();

        if (!IsPostBack)
        {
            DropDownList1.DataSource = ChungLoaiBUS.getAll();
            DropDownList1.DataTextField = "TenSP";
            DropDownList1.DataValueField = "MaSP";
            DropDownList1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GiangHangDTO ghDTO = new GiangHangDTO();
        ghDTO.TenSP = TextBox1.Text;
        if (FileUpload1.HasFile)
        {
            try
            {
                if (FileUpload1.PostedFile.ContentType == "image/jpeg")
                {
                    if (FileUpload1.PostedFile.ContentLength < 102400)
                    {

                        string filename = "NCC\\images_\\GiangHang\\" + FileUpload1.FileName;
                        FileUpload1.SaveAs(Server.MapPath("~/") + filename);
                        StatusLabel.Text = "File đã được upload!";

                    }
                    else
                        StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                }
                else
                    StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
        ghDTO.Hinh = FileUpload1.FileName;
        ghDTO.MaCL = long.Parse(DropDownList1.SelectedValue.ToString());
        ghDTO.Gia = int.Parse(txtgiaban.Text);
        ghDTO.MoTa = txtmota.Text;
        ghDTO.SoLuong = int.Parse(txtsoluong.Text);
        ghDTO.NgayCapNhat = ngayCapNhat.SelectedDate;
        string userName = Session["NCC_UN"].ToString();
        ghDTO.UserNCC = userName;
        int n = ghBUS.Insert(ghDTO);
        if (n == 1)
        {
            Response.Write("<script> alert('đã thêm thành công!!!!.')</script>");
            DataTable list = ghBUS.getAll();
            GridView1.DataSource = list;
        }
        else
        {
            Response.Write("<script> alert('Thêm không thành công!!!!.')</script>");
        }
    }
}